package com.bsoft.gol.hcb.dao.evaluation;

import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.spring.boot.annotation.SsdevDao;
import ctd.util.converter.ConversionUtils;
import hcn.util.CommonUtils;
import org.apache.commons.lang3.StringUtils;
import org.atag.dao.util.CustomAliasToEntityMapResultTransformer;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.transform.Transformers;
import pcn.bean.consultation.EvaluationLabelBean;
import pcn.bean.consultation.PatientEvaluateDetailQo;
import pcn.consultation.BaseEvaluationLabelrecord;
import pcn.vo.consultation.BaseEvaluationLabelrecordVo;
import pcn.vo.consultation.PatientEvaluateDetailVo;
import pcn.vo.sign.BaseEvaluationLabelVo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@SsdevDao(serviceId = "baseEvaluationLabelrecordDAOService")
public abstract class BaseEvaluationLabelrecordDAO extends HibernateSupportDelegateDAO<BaseEvaluationLabelrecord> {
    public BaseEvaluationLabelrecordDAO() {
        this.setKeyField("id");
    }

    public List<EvaluationLabelBean> queryBaseEvaluationLabelRecord(final int recordId) {
        HibernateStatelessResultAction<List<EvaluationLabelBean>> action =
                new AbstractHibernateStatelessResultAction<List<EvaluationLabelBean>>() {
                    @Override
                    public void execute(StatelessSession ss) throws Exception {
						StringBuffer hsql = new StringBuffer();
						hsql.append("select label_id as id,content from base_evaluation_labelrecord ");
						hsql.append("where record_id=:recordId");
						Query query = ss.createSQLQuery(hsql.toString());
						query.setParameter("recordId", recordId);
						query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
						List list = query.list();
						if (list.size() == 0) {
							setResult(null);
						} else {
							List<EvaluationLabelBean> evaluationLabelBeanList = new ArrayList<>();
							for (Object o : list) {
								evaluationLabelBeanList.add(ConversionUtils.convert(o, EvaluationLabelBean.class));
                            }
                            setResult(evaluationLabelBeanList);
                        }
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

	public List<BaseEvaluationLabelrecordVo> queryDoctorEvaluateLabelList(final String objectId, final List<String> recordIdList) {
		HibernateStatelessResultAction<List<BaseEvaluationLabelrecordVo>> action =
                new AbstractHibernateStatelessResultAction<List<BaseEvaluationLabelrecordVo>>() {
			@Override
			public void execute(StatelessSession ss) throws Exception {
				StringBuffer sql = new StringBuffer();
				sql.append("select id,record_id,label_id,content,evaluate_dt from base_evaluation_labelrecord where 1=1  ");
				if(CommonUtils.isNotEmpty(recordIdList)){
					sql.append(" and record_id in(:recordIdList) ");
				}
				if(StringUtils.isNotEmpty(objectId)){
					sql.append("and  record_id in (select id from base_evaluation_record where 1=1 and object_id=:objectId and verify_state='1' and business_type='3') ");
				}
				sql.append(" group by content,id,record_id,label_id,evaluate_dt ");
				Query query = ss.createSQLQuery(sql.toString());
				if(CommonUtils.isNotEmpty(recordIdList)){
					query.setParameterList("recordIdList", recordIdList);
				}
				if(StringUtils.isNotEmpty(objectId)){
					query.setParameter("objectId", objectId);
				}

				query = (SQLQuery) query.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
				List<Map> dbList = query.list();
				List<BaseEvaluationLabelrecordVo> list = new ArrayList<>();
				if (CommonUtils.isNotEmpty(dbList)) {
					for (Map map : dbList) {
						list.add(ConversionUtils.convert(map, BaseEvaluationLabelrecordVo.class));
					}
					setResult(list);
				} else {
					setResult(null);
				}
			}
		};
		HibernateSessionTemplate.instance().execute(action);
		return action.getResult();
	}

    public Long countDoctorEvaluateLabel(final String objectId, final String content, final List<String> recordIdList) {
        HibernateStatelessResultAction<Long> action = new AbstractHibernateStatelessResultAction<Long>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer sql = new StringBuffer();
				sql.append("select COUNT(1) labelCount from base_evaluation_record a ,base_evaluation_labelrecord b " +
						"where a.id = b.record_id and b.content=:content and a.verify_state='1' ");
                if (StringUtils.isNotEmpty(objectId)) {
					sql.append(" and a.object_id=:objectId ");
                }
                if (CommonUtils.isNotEmpty(recordIdList)) {
                    sql.append("and a.id in(:recordIdList) ");
                }
                Query query = ss.createSQLQuery(sql.toString());
                query.setParameter("content", content);
                if (StringUtils.isNotEmpty(objectId)) {
                    query.setParameter("objectId", objectId);
                }
                if (CommonUtils.isNotEmpty(recordIdList)) {
                    query.setParameterList("recordIdList", recordIdList);
                }
                query = (SQLQuery) query.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
                List<Map> dbList = query.list();
                Long count = 0L;
                if (CommonUtils.isNotEmpty(dbList)) {
                    count = Long.valueOf(String.valueOf(dbList.get(0).get("labelCount")));

                }
                setResult(count);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    public List<PatientEvaluateDetailVo> queryPatientEvaluateList(final PatientEvaluateDetailQo qo) {
        HibernateStatelessResultAction<List<PatientEvaluateDetailVo>> action =
                new AbstractHibernateStatelessResultAction<List<PatientEvaluateDetailVo>>() {
			@Override
			public void execute(StatelessSession ss) throws Exception {
				StringBuffer sql = new StringBuffer();
				sql.append("select r.id,r.person_name as patientName,r.content,r.add_time as evaluateTime" +
						" from base_evaluation_record r  where 1=1 ");
				if(StringUtils.isNotEmpty(qo.getDoctorId())){
					sql.append(" and r.object_id=:doctorId ");
                }
				if(StringUtils.isNotEmpty(qo.getDeptId())){
					sql.append(" and r.object_id=:deptId ");
                }
				if(CommonUtils.isNotEmpty(qo.getRecordIdList())){
                    sql.append(" and r.id in(:recordIdList) ");
                }
				sql.append(" and r.verify_state='1' order by evaluateTime desc ");
				Query query = ss.createSQLQuery(sql.toString());
				if(StringUtils.isNotEmpty(qo.getDoctorId())){
					query.setParameter("doctorId", qo.getDoctorId());
				}
				if(StringUtils.isNotEmpty(qo.getDeptId())){
					query.setParameter("deptId", qo.getDeptId());
				}
				if(CommonUtils.isNotEmpty(qo.getRecordIdList())){
					query.setParameterList("recordIdList", qo.getRecordIdList());
				}

				query.setFirstResult((qo.getPageNo() - 1) * qo.getPageSize());
				query.setMaxResults(qo.getPageSize());
				query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
				List<Map> resultList = query.list();
				if (resultList.size() == 0) {
					setResult(null);
				} else {
					List<PatientEvaluateDetailVo> list = new ArrayList<>();
					for (Map map : resultList) {
						list.add(ConversionUtils.convert(map, PatientEvaluateDetailVo.class));
					}
					setResult(list);
				}
			}
		};
		HibernateSessionTemplate.instance().execute(action);
		return action.getResult();
	}

	public List<BaseEvaluationLabelVo> queryBaseEvaluationLabelrecordList(final int recordId) {
		HibernateStatelessResultAction<List<BaseEvaluationLabelVo>> action =
                new AbstractHibernateStatelessResultAction<List<BaseEvaluationLabelVo>>() {
			@Override
			public void execute(StatelessSession ss) throws Exception {
				StringBuffer sql = new StringBuffer();
				sql.append("select id,content from base_evaluation_labelrecord  where record_id=:recordId");
				Query query = ss.createSQLQuery(sql.toString());
				query.setParameter("recordId", recordId);
				query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
				List<Map> resultList = query.list();
				if (resultList.size() == 0) {
					setResult(null);
				} else {
					List<BaseEvaluationLabelVo> list = new ArrayList<>();
					for (Map map : resultList) {
						list.add(ConversionUtils.convert(map, BaseEvaluationLabelVo.class));
					}
					setResult(list);
				}
			}
		};
		HibernateSessionTemplate.instance().execute(action);
		return action.getResult();
	}
}